package com.aaa.three.service.impl;

import com.aaa.three.entity.AddReg;
import com.aaa.three.entity.DoctorPrice;
import com.aaa.three.mapper.PatientRegistrationMapper;
import com.aaa.three.mapper.ResultMapper;
import com.aaa.three.pay.AliPayBean;
import com.aaa.three.pay.Alipay;
import com.aaa.three.pay.OutPay;
import com.aaa.three.pojo.domain.UserEmp;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.PayService;
import com.aaa.three.service.ResultService;
import com.aaa.three.sysconst.UserConst;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import static com.alibaba.druid.sql.parser.Token.BY;

/**
 * @PACKAGE_NAME: com.aaa.three.service.impl
 * @NAME: PayServiceImpl
 * @USER: Tprairie
 * @DATE: 2022/2/25
 * @PROJECT_NAME: three
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private PatientRegistrationMapper patientRegistrationMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private Alipay alipay;
    @Autowired
    private ResultMapper resultMapper;
    @Override
    public String payPat(AddReg addReg) throws AlipayApiException {
        UserEmp attribute = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = attribute.getId();
        addReg.setId(id);
        DoctorPrice sprice = patientRegistrationMapper.getSprice(addReg);
        String price = String.valueOf(sprice.getPrice());
        AddReg addReg1=patientRegistrationMapper.getRegistrationDoctor(addReg);
        Integer id1 = addReg1.getId();
        addReg.setSid(id1);
        Integer i=patientRegistrationMapper.addRegistration(addReg);
        String aid = String.valueOf(addReg.getId());
        if(i>0){
            if(addReg.getDay()==0){
                Integer upname = addReg1.getUpname();
                Integer totalnumber = addReg1.getTotalnumber();
                Integer lastnumber = addReg1.getLastnumber();
                Integer t=totalnumber-1;
                Integer l=lastnumber+1;
                Integer u=upname-1;
                addReg.setUpname(u);
                addReg.setTotalnumber(t);
                addReg.setLastnumber(l);
                Integer a= patientRegistrationMapper.updateUpSchedule(addReg);
                if(a>0){
                    AliPayBean aliPayBean = new AliPayBean();
                    aliPayBean.setOut_trade_no(aid);
                    aliPayBean.setSubject("Registration");
                    aliPayBean.setTotal_amount(price);
                    return alipay.pay(aliPayBean);
                }
                return null;
            }else {
                Integer downname = addReg1.getDownname();
                Integer totalnumber = addReg1.getTotalnumber();
                Integer lastnumber = addReg1.getLastnumber();
                Integer t=totalnumber-1;
                Integer l=lastnumber+1;
                Integer d=downname-1;
                addReg.setDownname(d);
                addReg.setTotalnumber(t);
                addReg.setLastnumber(l);
                Integer a= patientRegistrationMapper.updateDownSchedule(addReg);
                if(a>0){
                    AliPayBean aliPayBean = new AliPayBean();
                    aliPayBean.setOut_trade_no(aid);
                    aliPayBean.setSubject("Registration");
                    aliPayBean.setTotal_amount(price);
                    return alipay.pay(aliPayBean);
                }
                return null;
            }

        }
        return null;
    }

    @Override
    public String payAgain(Integer id,Integer price) throws AlipayApiException {
        Integer i=patientRegistrationMapper.payAgain(id);
        if(i>0){
            AliPayBean aliPayBean = new AliPayBean();
            aliPayBean.setOut_trade_no(String.valueOf(id));
            aliPayBean.setSubject("Registration");
            aliPayBean.setTotal_amount(String.valueOf(price));
            return alipay.pay(aliPayBean);
        }
        return null;
    }

    @Override
    public void outPay(Integer id, Integer price, HttpServletResponse response) throws Exception {
        AddReg deleteResult=resultMapper.querySchedule(id);
        Integer i=resultMapper.deleteRegistered(id);
        if(i>0){
            if(deleteResult.getDay()==0){
                Integer upname = deleteResult.getUpname();
                Integer a=upname+1;
                deleteResult.setUpname(a);
                Integer totalnumber = deleteResult.getTotalnumber();
                Integer b=totalnumber+1;
                deleteResult.setTotalnumber(b);
                Integer lastnumber= deleteResult.getLastnumber();
                Integer c=lastnumber-1;
                deleteResult.setLastnumber(c);
                Integer d= resultMapper.updateUpSchedule(deleteResult);
            }else{
                Integer downname = deleteResult.getDownname();
                Integer a=downname+1;
                deleteResult.setDownname(a);
                Integer totalnumber = deleteResult.getTotalnumber();
                Integer b=totalnumber+1;
                deleteResult.setTotalnumber(b);
                Integer lastnumber= deleteResult.getLastnumber();
                Integer c=lastnumber-1;
                deleteResult.setLastnumber(c);
                Integer d= resultMapper.updateDownSchedule(deleteResult);

            }

        }
        OutPay.out(id,price,response);
    }
}
